<?php
/**
 * AJAX Handler for Sites: Delete - Delete a site from the database for a project.
 * NOTE:    A Site has been standardised from Websites, Documentation Links, and Blog Links.
 *	    This is because they are all in the same format. A Change here affects all 3.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */
    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }
    
    if(isset($gVAR['type']) === true) {
	$lister = 'parent.SiteEDJX.list(\''.$gVAR['type'].'\');';
    } else {
	$lister = '';
    }
    $return_ok = <<<EOOK
	<!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">
		parent.EDJXConfirmer.show_ok();
		$lister
	    </script>
	    </body>
	</html>
EOOK;
    $return_nok = <<<EONOK
    <!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">
		parent.EDJXConfirmer.show_nok();
		$lister
	    </script>
	    </body>
	</html>
EONOK;
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($pVAR['del_item']) !== false && isset($gVAR['type']) !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    $url = $pVAR['del_item'];
	    $VALID_URL = filter_var($url, FILTER_VALIDATE_URL);
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true && $VALID_URL !== false) {
		$safe_url = $sql_conx->real_escape_string($url);
		if($gVAR['type'] == 'site') {
		   $vals = $Project->getWebsites();
		   $sql_table = 'tool_websites';
		   $sql_column = 'website';
		   $del_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::DelSite : ProjectEditType::AdminDelSite);
		} elseif($gVAR['type'] == 'docs') {
		    $vals = $Project->getDoclinks();
		    $sql_table = 'tool_doclinks';
		    $sql_column = 'doclink';
		    $del_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::DelDocs : ProjectEditType::AdminDelDocs);
		} elseif($gVAR['type'] == 'blog') {
		    $vals = $Project->getBlogs();
		    $sql_table = 'tool_blogs';
		    $sql_column = 'blogurl';
		    $del_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::DelBlog : ProjectEditType::AdminDelBlog);
		} elseif($gVAR['type'] == 'rss') {
		    $vals = $Project->getRSSfeeds();
		    $sql_table = 'tool_rssfeeds';
		    $sql_column = 'rssuri';
		    $del_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::DelRSS : ProjectEditType::AdminDelRSS);
		} else {
		    header("HTTP/1.1 400 Bad Request");
		    die($return_nok);
		}
		if(in_array($url, $vals) === true) {
		    $query = $sql_conx->query("DELETE FROM `{$sql_table}` WHERE `pid`='{$Project->getID()}' AND `{$sql_column}`='{$safe_url}'");
		    $Project->updateTriggered();
		    $Project->updateWrite($_LOGGEDINUSER->get_userid(), $del_prjid, array($pVAR['del_item']));
		    header("HTTP/1.1 200 OK");
		    die($return_ok);
		} else {}
	    } else {
		throw new InvalidProjectException();
	    }
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die($return_nok);
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die($return_nok);
    }
    ?>